﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using Meraz.Core.Repositories;

namespace Meraz.Core.Managers
{
    public class LinkManager
    {
        public UrlManager UrlManager { get; set; }
        public ILinkRepository LinkRepository { get; set; }
        
        public Link Load(Link link)
        {
            Link found = null;
            if (link.ID > 0){
                found = LinkRepository.GetById(link.ID);
            }else if (link.Post.ID > 0 && !string.IsNullOrEmpty(link.Url.Name)){
                found = LinkRepository.GetByHref(link.Post.ID, link.Url.Name);
            }
            if (found == null)
            {
                link.Url = UrlManager.Load(link.Url.Name);
                return link;
            }
            else
            {
                return found;
            }
        }
    }
}
